# -*- coding: utf-8 -*-

from typing import Any, Callable
from threading import Event as TEvent, Lock


class Result:

    def __init__(self) -> None:
        """ """
        self._lock = Lock()
        self._value = []
        self._e = TEvent()

    def set_value(self, value):
        """ """
        with self._lock:
            self._value.append(value)
        self._e.set()

    def result(self):
        """ """
        self._e.wait()
        return self._value

    def __iter__(self):
        """ """
        return iter(self._value)


class EventData:

    def __init__(self, *, data: Any = None, func: Callable = None) -> None:
        """ 
        ::param data : 请求参数
        ::param func : 回调函数
        """
        self._func = func
        self.data = data
        self._result = Result()

    def invoke(self):
        """ """
        if self._func is None:
            return
        return self._func()

    @property
    def result(self):
        """ """
        return self._result.result()

    def set_result(self, data):
        """ """
        self._result.set_value(data)


class Event:

    __slots__ = ("event_type", "event_data")

    def __init__(self, event_type: str, event_data: EventData) -> None:
        """
        ::param event_type
        ::param event_data
        """
        self.event_type: str = event_type
        self.event_data: EventData = event_data
